const cheerio = require("cheerio");
const { fetchHtml } = require("./utils");
const source = require("./source");

const getPageContent = async (params) => {
	const content = await getDeepthPage(params.url);
	if (!content) {
		return false;
	}
	const result = handlerPage(content, params.url);
	return result;
};

const getDeepthPage = async (url, content = {}) => {
	try {
		const res = await fetchHtml(url);
		if (res) {
			const parse = new URL(url);
			const targetSource = source.find((i) => i.origin === parse.origin);
			if (targetSource) {
				const $ = cheerio.load(res);
				const { wrapContainer, titleContainer, pagination, lineBreak } =
					targetSource.content.rules;

				if (!content.isParse) {
					content = {
						isParse: true,
						text: "",
						title: $(titleContainer.selector).text() || "",
					};
				}
				if (lineBreak === "br") {
					const txtElement = $(wrapContainer.selector);
					// 将 <br> 标签替换为 \n
					const modifiedHtml = txtElement
						.html()
						.replace(/<br\s*\/?>/g, "\n");
					// 获取修改后的文本内容
					const text = cheerio.load(modifiedHtml).text();
					content.text += text;
				} else {
					$(wrapContainer.selector).each((index, element) => {
						const text = $(element).text();
						content.text += `${text}\n`;
					});
				}

				if (pagination) {
					const nextPagination = $(pagination.selector).last();
					const nextPath = nextPagination.attr("href");
					const nextPathText = nextPagination.text().trim();

					if (nextPath.startsWith("/") && nextPathText === "下一页") {
						content = await getDeepthPage(
							`${parse.origin}${nextPath}`,
							content
						);
					}
				}
			} else {
				return false;
			}
		} else {
			return false;
		}
	} catch (error) {
		console.log(error);
		return false;
	}

	return content;
};

const handlerPage = (content, url) => {
	const parse = new URL(url);
	const targetSource = source.find((i) => i.origin === parse.origin);
	let handler = targetSource?.content?.handler;

	let { text, title } = content;
	text = text.replace(/\n\s*\n/g, "\n");
	const p = text
		.split(/\n/)
		.map((i) => i.trim())
		.filter((i) => i);
	handler?.replace?.forEach((r) => {
		if (r.type === "Array") {
			p.splice(r.value, 1);
		}
	});

	return {
		data: p,
		text,
		title,
	};
};

module.exports = {
	getPageContent,
};
